<!DOCTYPE html>
<html lang="en">
<!-- 段彦蓉 -->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加院线</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <style>
        .img-area {
            margin: 10px;
            position: relative;
            width: 150px;
            height: 150px;
            border: 1px solid;
            border-radius: 5px;
            display: none;
        }

        .img-area i {
            position: absolute;
            right: 5px;
            top: 2px;
            color: #fff;
            z-index: 1;
            transition: all 1s;
        }

        .img-area i:hover {
            transform: rotate(180deg);
        }

        .img-area::after {
            content: "";
            position: absolute;
            height: 20px;
            background: rgba(0, 0, 0, 0.3);
            top: 0;
            left: 0;
            width: 151px;
            border-radius: 5px 5px 0 0;
        }

        .img-area img {
            width: 100%;
            height: 100%;
            display: block;
            object-fit: contain;
            border-radius: 5px;
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <div class="layui-layout layui-layout-admin">
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <form class="layui-form" action="" lay-filter="addForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">院线名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="txtPwd" required lay-verify="required" placeholder="请输入院线名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">院线地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="addr" id="txtPwd"  lay-verify="required" placeholder="请输入院线地址" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">图片</label>
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                            <div class="img-area">
                                <img src="" alt="">
                                <i class="layui-icon layui-icon-close"></i>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="tel" id="txtPwd" required lay-verify="number" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" id="addBtn" class="layui-btn" lay-submit lay-filter="formDemo">添加院线</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button class="layui-btn layui-btn-normal add" onclick="location.href='./Cinema.html'">返回</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <table id="tab" lay-filter="class"></table>
    <script src="../layui/layui.js"></script>
    <script src="../js/lodash.min.js"></script>
    <script src="../js/jquery-1.12.4.min.js"></script>
    <script src="../js/common.js"></script>
    <script>
        layui.use(['form', 'layer', 'upload'], function () {
            let image;
            const form = layui.form;
            const layer = layui.layer;
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: "#test1",//绑定元素
                url: "/api/upload",//上传接口
                field: "image",
                acceptMime: "image/*",//只能显示图片
                size: 500,//限制图片的大小
                multiple: true,//可以多选
                done: function (res) {
                    //上传完毕回调
                    $(".img-area img").prop("src", res.data[0]);
                    image = res.data[0];
                    $(".img-area").show();
                },
                error: function () {
                    // 请求异常回调
                    console.log("错误");
                }
            })
            //监听提交
            form.on('submit', async function (data) {
                if (!image) {
                    layer.msg("请上传图片封面！")
                    return;
                }
                data.field.image = image;
                const res = await $.post("/api/Cinema", data.field);
                if (res) {
                    layer.msg('添加院线成功', {
                        icon: 1,
                        time: 1000
                    }, function () {
                        location.href = './Cinema.html';
                    });
                }
            })
        })

        $(".img-area i").click(function () {
            $(".img-area img").prop("src", "");
            $(".img-area").hide();
        })

    </script>
</body>

</html>